package com.icedbrew.appfuel.persistence.entity;

import com.icedbrew.appfuel.util.StringUtil;

import javax.persistence.*;
import java.math.BigDecimal;

/**
 * An entity that can be used in many situations such as configuration options,
 * drop-down items, and the like. Application options are categorized by type.
 *
 * @see ApplicationOptionType
 * @author  arjay.nacion
 * @since   1.0
 */
@Entity
@Table(name = "application_options", uniqueConstraints =
	@UniqueConstraint(columnNames = { "code", "type" }))
public class ApplicationOption extends BaseEntity {

	@Column(name = "code", length = 75, nullable = false)
	private String code = StringUtil.EMPTY_STRING;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "type", nullable = false)
	private ApplicationOptionType type;

	@Column(name = "text_value", length = 255)
	private String textValue = StringUtil.EMPTY_STRING;

	@Column(name = "boolean_value")
	private Boolean booleanValue = Boolean.FALSE;

	@Column(name = "numeric_value", precision = 19, scale = 2)
	private BigDecimal numericValue = BigDecimal.ZERO;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public ApplicationOptionType getType() {
		return type;
	}

	public void setType(ApplicationOptionType type) {
		this.type = type;
	}

	public String getTextValue() {
		return textValue;
	}

	public void setTextValue(String textValue) {
		this.textValue = textValue;
	}

	public Boolean getBooleanValue() {
		return booleanValue;
	}

	public void setBooleanValue(Boolean booleanValue) {
		this.booleanValue = booleanValue;
	}

	public BigDecimal getNumericValue() {
		return numericValue;
	}

	public void setNumericValue(BigDecimal numericValue) {
		this.numericValue = numericValue;
	}

	@Override
	public final boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof ApplicationOption)) return false;
		if (!super.equals(o)) return false;

		ApplicationOption that = (ApplicationOption) o;

		if (code != null ? !code.equals(that.code) : that.code != null)
			return false;
		if (type != null ? !type.equals(that.type) : that.type != null)
			return false;

		return true;
	}

	@Override
	public final int hashCode() {
		int result = super.hashCode();
		result = 31 * result + (code != null ? code.hashCode() : 0);
		result = 31 * result + (type != null ? type.hashCode() : 0);
		return result;
	}
}
